<?php

	class shops {
		

		function shops() {
			global $admin;

			//if(empty($_SESSION["managerid"])) $admin->login();
		}
		
		function index() {
			global $admin,$smarty;
			//if(!$admin->rights("shops_view")) return;
			$shop_type = array(
			    '0'=>'未指定',
			    '1'=>'现货',
			    '2'=>'保税仓',
			    '3'=>'直邮',
			);
			$smarty->assign("shop_type", $shop_type);
		}

		function create() {
			global $db, $smarty, $config, $admin;
			//if(!$admin->rights("shops_create")) return;
			
			if(!empty($_POST["action"]) && $_POST["action"]=="create") {
				if(empty($_POST["title"])) $errors["empty_title"] = true;
				if(empty($_POST["show_title"])) $errors["empty_show_title"] = true;
				if(empty($errors) && $db->getone("select count(1) from {$config->db_prefix}shops where title=".$db->qstr($_POST["title"]))) $errors["exist_title"] = true;

				if(empty($errors)) {
					$title = $db->qstr($_POST["title"]);
					$show_title=$db->qstr($_POST['show_title']);
					$sqlstr = "insert into {$config->db_prefix}shops (title,show_title) values ($title,$show_title)";
					$result = $db->execute($sqlstr);
					if($result) header("location:{$config->site_admin}m=inventory&s=shops.php");
				}
								
				if(!empty($errors)) $smarty->assign("errors", $errors);								
			}
		}


		function modify() {
			global $db, $smarty, $config, $admin;
			//if(!$admin->rights("shops_modify")) return;
			
			if(!empty($_POST["action"]) && $_POST["action"]=="modify") {
				$id = $db->qstr($_POST["id"]);
				
				if(empty($_POST["title"])) $errors["empty_title"] = true;
				if(empty($_POST["show_title"])) $errors["empty_show_title"] = true;
				if(empty($errors) && $db->getone("select count(1) from {$config->db_prefix}shops where id!=$id and title=".$db->qstr($_POST["title"]))) $errors["exist_title"] = true;

				if(empty($errors)) {
					$title = $db->qstr($_POST["title"]);
					$show_title=$db->qstr($_POST['show_title']);
						
					
					$sqlstr = "update {$config->db_prefix}shops set title=$title,show_title=$show_title where id=$id";
					$result = $db->execute($sqlstr);
					if($result) header("location:".(empty($_POST["referer"])?"{$config->site_admin}m=inventory&s=shops.php":$_POST["referer"]));
				}
								
				if(!empty($errors)) $smarty->assign("errors", $errors);								
			}
		}


		function remove() {
			global $db, $segment, $config, $admin;
			//if(!$admin->rights("shops_delete")) return;

			$id = $db->qstr($_GET['id']);
			$sqlstr = "delete from {$config->db_prefix}shops where id=$id";
			$result = $db->execute($sqlstr);
			if($result) header("location:".(empty($_SERVER['HTTP_REFERER'])?"{$config->site_admin}m=inventory&s=shops.php":$_SERVER['HTTP_REFERER']));
		}


}
?>